﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.WebColumn
{
	/// <summary>
	/// 控制类
	/// </summary>
	/// <![CDATA[2014-8-1 16:52:14]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		private Config clsConfig = new Config();		// 配置类对象
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strColumnName", clsDatabase.COLUMNNAME, clsDatabase.listStructure[clsDatabase.COLUMNNAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nParentID", clsDatabase.PARENTID, clsDatabase.listStructure[clsDatabase.PARENTID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nGrade", clsDatabase.GRADE, clsDatabase.listStructure[clsDatabase.GRADE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nOrder", clsDatabase.ORDER, clsDatabase.listStructure[clsDatabase.ORDER].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nInherit", clsDatabase.INHERIT, clsDatabase.listStructure[clsDatabase.INHERIT].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nParentColumnID", clsDatabase.listStructure[clsDatabase.PARENTID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strColumnName", clsDatabase.listStructure[clsDatabase.COLUMNNAME].strName, "LIKE"));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nInherit", clsDatabase.listStructure[clsDatabase.INHERIT].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("string", "strColumnIDs", clsDatabase.listStructure[clsDatabase.ID].strName, "IN"));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 实体数据转换

		protected override TonyNet4V1.EntityBase DataToEntity(System.Data.DataRow dr)
		{
			Entity clsColumn = new Entity();
			Entity clsParent = new Entity();

			clsColumn = (Entity) base.DataToEntity(dr);

			clsParent = GetOne(clsColumn.ParentID);
			if (clsParent != null)
			{
				clsColumn.ParentName = clsParent.ColumnName;
			}

			return clsColumn;
		}
		protected override void EntityToData(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsParent = new Entity();
			Entity clsColumn = new Entity();

			clsColumn = (Entity)clsEntity;
			clsParent = GetOne(clsColumn.ParentID);
			if (clsParent != null)
			{
				// 有上级栏目
				clsColumn.Grade = clsParent.Grade + 1;
			}
			else
			{
				// 没有上级栏目
				clsColumn.Grade = clsConfig.nColumn_FirstGrade;
				clsColumn.Inherit = false;
			}

			base.EntityToData(clsColumn);
		}

		#endregion 实体数据转换
		/// <summary>
		/// 修改
		/// </summary>
		/// <![CDATA[2014-8-10 10:08:04]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public override bool Modify(TonyNet4V1.EntityBase clsEntity)
		{
			Entity clsColumn = new Entity();
			Entity clsOldColumn = new Entity();
			List<Entity> list = new List<Entity>();
			QueryEntity clsQuery = new QueryEntity();
			bool bReturn = false;

			clsOldColumn = GetOne(((Entity) clsEntity).ID);

			bReturn = base.Modify(clsEntity);

			clsColumn = GetOne(((Entity)clsEntity).ID);
			
			if (clsColumn.Grade != clsOldColumn.Grade)
			{
				clsQuery.nParentColumnID = clsColumn.ID;
				list = Query(clsQuery);
				for (int i = 0; i < list.Count; i++)
				{
					Modify(list[i]);
				}
			}

			return bReturn;
		}

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-1 16:52:14]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}

		#endregion 查询

		#region 查询所有栏目，并按照栏目等级排序，每一个高等级栏目紧跟着它下级所有栏目
		/// <summary>
		/// 查询所有栏目，并按照栏目等级排序，每一个高等级栏目紧跟着它下级所有栏目
		/// </summary>
		/// <![CDATA[2014-8-9 14:40:57]]>
		/// <returns></returns>
		public List<Entity> QueryAllOrder()
		{
			List<Entity> list = new List<Entity>();

			QueryByParentOrderByGrade(clsConfig.nColumn_NoParent, list);

			return list;
		}
		#endregion 查询所有栏目，并按照栏目等级排序，每一个高等级栏目紧跟着它下级所有栏目

		/// <summary>
		/// 查询不继承上级栏目权限的所有栏目
		/// </summary>
		/// <![CDATA[2014-8-13 18:20:17]]>
		/// <returns></returns>
		public List<Entity> QueryNoInheritColumn()
		{
			return QueryByInherit(clsConfig.nColumn_NoInherit);
		}

		/// <summary>
		/// 根据是否继承上级栏目权限查询栏目，并按等级排序
		/// </summary>
		/// <![CDATA[2014-8-13 18:18:21]]>
		/// <param name="nInherit"></param>
		/// <returns></returns>
		private List<Entity> QueryByInherit(int nInherit)
		{
			List<Entity> list = new List<Entity>();
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nInherit = nInherit;
			QueryByParentOrder(clsConfig.nColumn_NoParent, list, clsQuery);

			return list;
		}

		/// <summary>
		/// 根据上级栏目ID查询栏目（后台栏目）
		/// </summary>
		/// <![CDATA[2014-8-3 18:15:42]]>
		/// <param name="nParentColumnID">上级栏目</param>
		/// <param name="strColumnIDs">用户有权限的栏目ID字符串</param>
		/// <returns></returns>
		public List<Entity> QueryByParent(int nParentColumnID)
		{
			List<Entity> list = new List<Entity>();
			QueryEntity clsQuery = new QueryEntity();

			clsQuery.nParentColumnID = nParentColumnID;

			return Query(clsQuery);
		}
		
		/// <summary>
		/// 根据上级栏目ID查询本级及下级栏目，把结果添加到list中
		/// </summary>
		/// <![CDATA[2014-8-10 11:31:01]]>
		/// <param name="nParentColumnID"></param>
		/// <param name="list"></param>
		/// <param name="clsQuery"></param>
		private void QueryByParentOrderByGrade(int nParentColumnID, List<Entity> list, QueryEntity clsQuery)
		{
			List<Entity> listQuery = new List<Entity>();

			clsQuery.nParentColumnID = nParentColumnID;

			listQuery = Query(clsQuery);
			for (int i = 0; i < listQuery.Count; i++)
			{
				list.Add(listQuery[i]);

				QueryByParentOrderByGrade(listQuery[i].ID, list, clsQuery);
			}
		}

		/// <summary>
		/// 根据上级栏目ID，和其它查询条件查询栏目，并按照栏目等级排序
		/// </summary>
		/// <![CDATA[2014-8-13 18:10:41]]>
		/// <param name="nParentColumnID"></param>
		/// <param name="list"></param>
		/// <param name="clsQuery"></param>
		private void QueryByParentOrder(int nParentColumnID, List<Entity> list, QueryEntity clsQuery)
		{
			List<Entity> listQuery = new List<Entity>();

			clsQuery.nParentColumnID = nParentColumnID;

			listQuery = Query(clsQuery);
			for (int i = 0; i < listQuery.Count; i++)
			{
				list.Add(listQuery[i]);

				QueryByParentOrder(listQuery[i].ID, list, clsQuery);
			}
		}

		/// <summary>
		/// 根据上级栏目ID查询本级及下级栏目，把结果添加到list中
		/// </summary>
		/// <![CDATA[2014-8-3 14:27:53]]>
		/// <param name="nParentColumnID">上级栏目ID</param>
		/// <param name="list">总查询结果</param>
		private void QueryByParentOrderByGrade(int nParentColumnID, List<Entity> list)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> listQuery = new List<Entity>();

			clsQuery.nParentColumnID = nParentColumnID;

			listQuery = Query(clsQuery);
			for (int i = 0; i < listQuery.Count; i++)
			{
				list.Add(listQuery[i]);

				QueryByParentOrderByGrade(listQuery[i].ID, list);
			}
		}

		

		#region 把栏目链表根据各个栏目等级，在栏目名称前加上对应等级的符号。
		/// <summary>
		/// 把栏目链表根据各个栏目等级，在栏目名称前加上对应等级的符号。
		/// </summary>
		/// <![CDATA[2014-8-10 09:57:54]]>
		public void AddGradeInName(List<Entity> list, string strFirstMark, string strGradeMark)
		{
			string strMark = "";

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsColumn = list[i];
				strMark = "";
				for (int j = 1; j < clsColumn.Grade; j++)
				{
					strMark = strMark + strGradeMark;
				}
				if (clsColumn.Grade > clsConfig.nColumn_FirstGrade)
				{
					clsColumn.ColumnName = strFirstMark + strMark + "  " + clsColumn.ColumnName;
				}
				else
				{
					clsColumn.ColumnName = strMark + "  " + clsColumn.ColumnName;
				}
			}
		}
		#endregion 把栏目链表根据各个栏目等级，在栏目名称前加上对应等级的符号。

		#region 根据栏目ID获得栏目名称，例如栏目ID：1, 2, 3获得栏目字符串：栏目， 栏目， 栏目
		/// <summary>
		/// 根据栏目ID获得栏目名称，例如栏目ID：1, 2, 3获得栏目字符串：栏目， 栏目， 栏目
		/// </summary>
		/// <![CDATA[2014-8-14 09:46:07]]>
		/// <param name="strColumnIDs"></param>
		/// <returns></returns>
		public string QueryColumnNamesByColumnIDs(string strColumnIDs)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();
			string strColumnNames = "";

			if (strColumnIDs != "")
			{
				AddInheritColumnIDToColumnIDs(ref strColumnIDs);

				clsQuery.strColumnIDs = strColumnIDs;
				//list = Query(clsQuery);
				//list = QueryAdminByParentRight(clsConfig.nColumn_NoParent, strColumnIDs);
				QueryByParentOrder(clsConfig.nColumn_NoParent, list, clsQuery);

				for (int i = 0; i < list.Count; i++)
				{
					strColumnNames = strColumnNames + list[i].ColumnName + ", ";
				}

				strColumnNames = TonyNet4V1.CommonFunction.ExceptString(", ", strColumnNames);

				return strColumnNames;
			}
			else
			{
				return "";
			}
		}
		#endregion 根据栏目ID获得栏目名称，例如栏目ID：1, 2, 3获得栏目字符串：栏目， 栏目， 栏目

		#region 查询用户有权限的所有栏目
		/// <summary>
		/// 查询用户有权限的所有栏目
		/// </summary>
		/// <![CDATA[2014-8-17 16:04:53]]>
		/// <param name="strWebColumnIDs">用户有权限的栏目ID字符串</param>
		/// <returns></returns>
		public List<Entity> QueryByRight(string strWebColumnIDs)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			AddInheritColumnIDToColumnIDs(ref strWebColumnIDs);
			clsQuery.strColumnIDs = strWebColumnIDs;

			QueryByParentOrder(clsConfig.nColumn_NoParent, list, clsQuery);

			return list;
		}
		#endregion 查询用户有权限的所有栏目

		/// <summary>
		/// 根据前台栏目ID字符串，给该字符串添加继承栏目权限的栏目ID
		/// </summary>
		/// <![CDATA[2014-8-14 10:56:42]]>
		/// <param name="strColumnIDs"></param>
		public void AddInheritColumnIDToColumnIDs(ref string strColumnIDs)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.strColumnIDs = strColumnIDs;
			list = Query(clsQuery);
			for (int i = 0; i < list.Count; i++)
			{
				QueryByParentInherit(ref strColumnIDs, list[i].ID);
			}
		}

		/// <summary>
		/// 根据上级栏目查询继承上级权限的栏目，并把栏目ID添加到“strInheritColumnIDs”变量中。
		/// </summary>
		/// <![CDATA[2014-8-14 10:51:10]]>
		/// <param name="strInheritColumnIDs"></param>
		/// <param name="nParentColumnID"></param>
		private void QueryByParentInherit(ref string strInheritColumnIDs, int nParentColumnID)
		{
			QueryEntity clsQuery = new QueryEntity();
			List<Entity> list = new List<Entity>();

			clsQuery.nParentColumnID = nParentColumnID;
			clsQuery.nInherit = clsConfig.nColumn_Inherit;

			list = Query(clsQuery);
			for (int i = 0; i < list.Count; i++)
			{
				strInheritColumnIDs = strInheritColumnIDs + ", " + list[i].ID.ToString();
				QueryByParentInherit(ref strInheritColumnIDs, list[i].ID);
			}
		}

		/// <summary>
		/// 查询栏目和它的子栏目，组成查询字符串
		/// </summary>
		/// <![CDATA[2014-8-20 17:20:19]]>
		/// <param name="nColumnID">要查询的栏目ID</param>
		/// <returns>查询栏目和它的子栏目，组成查询字符串</returns>
		public string QueryColumnAndChildColumn(int nColumnID)
		{
			List<Entity> list = new List<Entity>();
			QueryEntity clsQuery = new QueryEntity();
			string strReturn = "";

			QueryByParentOrder(nColumnID, list, clsQuery);

			for (int i = 0; i < list.Count; i++)
			{
				strReturn = strReturn + list[i].ID.ToString() + ", ";
			}

			//strReturn = TonyNet4V1.CommonFunction.ExceptString(", ", strReturn);
			strReturn = strReturn + nColumnID.ToString();

			return strReturn;
		}

		#region 根据前台栏目ID获得该栏目的名称
		
		/// <summary>
		/// 根据前台栏目ID获得该栏目的名称
		/// </summary>
		/// <param name="nWebColumnID">前台栏目ID</param>
		/// <returns>前台栏目名称</returns>
		public string GetOneWebColumnName(int nWebColumnID)
		{
			Entity clsWebColumn = new Entity();

			clsWebColumn = GetOne(nWebColumnID);

			if (clsWebColumn != null)
			{
				return clsWebColumn.ColumnName;
			}
			else
			{
				return "";
			}
		}

		#endregion 根据前台栏目ID获得该栏目的名称
	}
}
